package inser;

import java.sql.Connection;

import java.sql.SQLException;

import com.datawarehouse.Connecteur;
import com.mysql.jdbc.Statement;
import com.outil.Outil;

import dto.dtoWahouse.Client;
import dto.dtoWahouse.Gite;

public class Insereur {
	/*
 * */
	public boolean inserClient(Connection _con, Client _client)
			throws ClassNotFoundException, SQLException {

		String query = this.getClientQuery(_client);

		return this.exectuteRequete(_con, query);
	}

	private String getClientQuery(Client _client) {
		String query = "INSERT INTO `client` (`FirstName_Client`,`Famil_Name_Client`,`Dat_of_birth_Client`,`Sex_Client`,`Situation_Client`,`Tel_Client`,`Email_Client`,`Fax_Client`,`Nationality _Client`,`Country_Client`,`Town_Client`,`Postal_Code_Client`,`Adress-Street_Client`,`Compagn_Number`)"
				+ "VALUES ('"
				+ _client.getFirstName()
				+ "', '"
				+ _client.getFamilyName()
				+ "', '2009-01-23', '"
				+ _client.getSexe()
				+ "', '"
				+ _client.getSituation()
				+ "', '"
				+ _client.getTelephone()
				+ "', '"
				+ _client.getEmail()
				+ "', '"
				+ _client.getFax()
				+ "', '"
				+ _client.getNationality()
				+ "', '"
				+ _client.getCountry()
				+ "', '"
				+ _client.getTownClient()
				+ "', '"
				+ _client.getCodePostalClient()
				+ "', '"
				+ _client.getAdresseStreetClient()
				+ "', '"
				+ _client.getCompanyNumber() + "');";
		return query;
	}

	public String getGiteQuery(Gite _gite) {
		String date = Outil.getDateFormat(_gite.getDateStartRentals());
		String query = "INSERT INTO `gite` (`gite_street` , `gite_code_postal` , `gite_town` , `gite_region` , `gite_country` , `firstnam_owner` , `familynam_owner` , `dat_start_rentals` , `tel_owner` , `surface` )VALUES ('"
				+ _gite.getGiteStreet()
				+ "', '"
				+ _gite.getCodePostaleGite()
				+ "', '"
				+ _gite.getGiteTown()
				+ "', '"
				+ _gite.getGiteRegion()
				+ " ', '"
				+ _gite.getGiteCountry()
				+ "', '"
				+ _gite.getFirstNameOwner()
				+ "', '"
				+ _gite.getFamilyOwner()
				+ "', '"
				+ date
				+ "','"
				+ _gite.getTelOwner()
				+ "', '"
				+ _gite.getSurface() + "' )";
		return query;

	}

	private boolean exectuteRequete(Connection _con, String _query)
			throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.jdbc.Driver");
		Connecteur connecteur = new Connecteur(
				"jdbc:mysql://127.0.0.1:3306/WAREHOUSE", "root", "");
		connecteur.openConnection();
		Connection con = connecteur.getConnection();
		Statement stmt = (Statement) con.createStatement();
		return stmt.execute(_query);
	}

	public boolean inserGite(Connection _con, Gite _gite)
			throws ClassNotFoundException, SQLException {
		String query = this.getGiteQuery(_gite);
		return this.exectuteRequete(_con, query);

	}

}
